﻿using System.Linq;

namespace System.Collections.Generic
{
    public static class EnumerableExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            } 
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            foreach (T item in items)
            {
                action(item);
            }
        }

        public static IEnumerable<T> PagedItems<T>(this IEnumerable<T> allItems, int pageSize, int currentPage, out int totalPages) where T : class
        {
            int len = allItems.Count();

            if ((len % pageSize) == 0)
            {
                totalPages = len / pageSize;
            }
            else
            {
                totalPages = len / pageSize + 1;
            }

            IEnumerable<T> items = allItems.Skip(pageSize * currentPage);
            items = items.Take(pageSize);

            return items;
        }

    }
}
